package demo.spark.stream.format;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/9/9 上午10:38
 * @UpdateDate:
 * @Description:
 */
@InterfaceAudience.Public
@InterfaceStability.Stable
public class PathAndOffsetWritable implements WritableComparable<PathAndOffsetWritable> {
    private long value;
    private Path filePath;

    public PathAndOffsetWritable() {}

    public PathAndOffsetWritable(long value, Path filePath) { set(value); this.filePath = filePath; }

    /** Set the value of this LongWritable. */
    public void set(long value) { this.value = value; }

    /** Return the value of this LongWritable. */
    public long get() { return value; }

    public Path getFilePath() { return filePath; }

    public void setFilePath(Path filePath) { this.filePath = filePath; }

    @Override
    public void readFields(DataInput in) throws IOException {
        value = in.readLong();
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(value);
    }

    /** Returns true iff <code>o</code> is a LongWritable with the same value. */
    @Override
    public boolean equals(Object o) {
        if (!(o instanceof PathAndOffsetWritable))
            return false;
        PathAndOffsetWritable other = (PathAndOffsetWritable)o;
        return this.value == other.value
                && Objects.equals(this.filePath.toString(),other.filePath.toString());
    }

    @Override
    public int hashCode() {
        return (int)value;
    }

    /** Compares two LongWritables. */
    @Override
    public int compareTo(PathAndOffsetWritable o) {
        long thisValue = this.value;
        long thatValue = o.value;
        return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
    }

    @Override
    public String toString() {
        return "Path:".concat(Objects.toString(filePath.toString())).concat(" offset:") + Long.toString(value);
    }

    /** A decreasing Comparator optimized for FileAndOffsetWritable. */
    public static class Comparator extends WritableComparator {

        @Override
        public int compare(WritableComparable a, WritableComparable b) {
            return super.compare(b, a);
        }
        @Override
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
            long thisValue = readLong(b1, s1);
            long thatValue = readLong(b2, s2);
            return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));
        }
    }

    // register default comparator
    static {
        WritableComparator.define(PathAndOffsetWritable.class, new PathAndOffsetWritable.Comparator());
    }
}
